package com.haitaoit.chengquan.module.home.adapter.forum;

import android.content.Context;
import android.content.Intent;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.haitaoit.chengquan.R;
import com.haitaoit.chengquan.base.BackCall;
import com.haitaoit.chengquan.module.home.model.ForumCenterModel;
import com.haitaoit.chengquan.ui.equipment.activity.ShowPictureActivity;
import com.haitaoit.chengquan.view.CircleImageView;
import com.haitaoit.chengquan.view.MyGridView;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.List;

/**
 * Created by Administrator on 2016/10/21.
 */
public class ForumCenterAdapter extends BaseAdapter {
    private Context context;
    private BackCall backCall;
    private List<ForumCenterModel> list;
    public void setBackCall(BackCall backCall) {
        this.backCall = backCall;
    }

    public ForumCenterAdapter(Context context) {
        this.context = context;
    }

    public List<ForumCenterModel> getList() {
        return list;
    }

    public void setList(List<ForumCenterModel> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        return list == null ? 0 : list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.forum_center_item, null);
            holder = new ViewHolder();
            ViewUtils.inject(holder, convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.forum_itemll.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ForumCenterAdapter.this.backCall.deal(R.id.forum_itemll, position);
            }
        });

        ImageLoader.getInstance().displayImage(list.get(position).getHead_portrait(), holder.forum_center_img);
        holder.forum_name.setText(list.get(position).getName());

        SpannableString msp = new SpannableString(list.get(position).getClass_name() +
                " | " + list.get(position).getTitle());
        msp.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 0,
                list.get(position).getClass_name().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); // 粗体
        msp.setSpan(new ForegroundColorSpan(context.getResources().getColor(R.color.app_color)), 0,
                list.get(position).getClass_name().length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        holder.forum_title.setText(msp);
        holder.forum_content.setText(list.get(position).getContent());
        holder.forum_addtime.setText(list.get(position).getAddtime());

           int count=Integer.parseInt(list.get(position).getVisit_count());
            Log.i("====","======"+count);
            if(count>0){
                holder.forum_visit_count.setVisibility(View.VISIBLE);
                holder.forum_visit_count.setText(count+"");
            }else{
                holder.forum_visit_count.setVisibility(View.GONE);
            }

        holder.reply.setText(list.get(position).getComments_count());

        if (list.get(position).getImg_list() == null || list.get(position).getImg_list().size() == 0) {
            holder.forum_gridview.setVisibility(View.GONE);
        } else {
            holder.forum_gridview.setVisibility(View.VISIBLE);
            ForumCenterImgAdapter adapter = new ForumCenterImgAdapter(context);
            adapter.setList(list.get(position).getImg_list());
            holder.forum_gridview.setAdapter(adapter);

            adapter.setBackCall(new BackCall() {
                @Override
                public void deal(int which, Object... obj) {
                    String imageUri = (String) obj[0];
                    switch (which) {
                        case R.id.icon:
                            Intent intent = new Intent();
                            intent.setClass(parent.getContext(), ShowPictureActivity.class);
                            intent.putExtra("image_uri", imageUri);
                            parent.getContext().startActivity(intent);
                            break;
                    }
                    super.deal(which, obj);
                }
            });
            holder.forum_gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                    ForumCenterAdapter.this.backCall.deal(R.id.forum_itemll, position);
                }
            });
        }
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.forum_itemll)
        LinearLayout forum_itemll;

        @ViewInject(R.id.forum_gridview)
        MyGridView forum_gridview;
        @ViewInject(R.id.forum_title)
        TextView forum_title;
        @ViewInject(R.id.forum_center_img)
        CircleImageView forum_center_img;
        @ViewInject(R.id.forum_name)
        TextView forum_name;
        @ViewInject(R.id.forum_content)
        TextView forum_content;
        @ViewInject(R.id.forum_addtime)
        TextView forum_addtime;
        @ViewInject(R.id.forum_visit_count)
        TextView forum_visit_count;
        @ViewInject(R.id.reply)
        TextView reply;
    }
}
